package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ListView;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class CommentPlaceLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
	private static String url_get_comment_place = IP.get_IP() +  IP.get_path_service() + "get_comment_place.php";
	
	 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_COMMENT = "comment";
    public final String TAG_USER_ID = "user_id_zing";
    public final String TAG_USER_NAME = "user_name_zing";
    public final String TAG_USER_DISPLAY_NAME = "user_display_name";
    public final String TAG_USER_AVATAR = "user_avatar_zing";
    public final String TAG_COMMENT_ID = "comment_id";
    public final String TAG_PLACE_ID = "place_id";
    public final String TAG_DETAIL_COMMENT = "detail_comment";
    public final String TAG_TIME_COMMENT = "time_comment";

    // products JSONArray
    JSONArray users = null;
    
    String place_id;
    
    public CommentPlaceLoader(String PlaceId)
    {
    	place_id = PlaceId;
    }
    
    public  ArrayList<HashMap<String, String>> GetCommentPlace()
    {
    	ArrayList<HashMap<String, String>> commentplacesList = new ArrayList<HashMap<String, String>>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("place_id", place_id));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_comment_place, "POST", params);

        Log.d("All comment: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	users = json.getJSONArray(TAG_COMMENT);

                // looping through All Comment
                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);

                    // Storing each json item in variable
                    String placeId = place_id;
                    String userId = c.getString(TAG_USER_ID);
                    String commentId = c.getString(TAG_COMMENT_ID);
                    String username = c.getString(TAG_USER_NAME);
                    String displayname = c.getString(TAG_USER_DISPLAY_NAME);
                    String avatar = c.getString(TAG_USER_AVATAR);
                    String detailComment = c.getString(TAG_DETAIL_COMMENT);
                    String timeComment = c.getString(TAG_TIME_COMMENT);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_USER_ID, userId);
                    map.put(TAG_USER_NAME, username);
                    map.put(TAG_USER_AVATAR, avatar);
                    map.put(TAG_COMMENT_ID, commentId);
                    map.put(TAG_PLACE_ID, placeId);
                    map.put(TAG_USER_DISPLAY_NAME, displayname);
                    map.put(TAG_DETAIL_COMMENT, detailComment);
                    map.put(TAG_TIME_COMMENT, timeComment);

                    // adding HashList to ArrayList
                    commentplacesList.add(map);
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return commentplacesList;
    }
}